package com.example.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.reggie.common.PageInfo;
import com.example.reggie.common.R;
import com.example.reggie.dto.DishDto;
import com.example.reggie.entity.Dish;
import com.example.reggie.entity.DishFlavor;
import com.example.reggie.service.CategoryServcie;
import com.example.reggie.service.DishFlavorService;
import com.example.reggie.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@Slf4j
@RequestMapping("/dish")
public class DishController {
    @Autowired
    private DishService dishService;
    @Autowired
    private DishFlavorService dishFlavorService;
    @Autowired
    private CategoryServcie categoryServcie;

    @PostMapping("")
    public R<String> addDish(@RequestBody DishDto dishDto) {
        log.info("开始增加菜品");
        dishService.addWithFlavor(dishDto);
        return R.success("增加成功");
    }

    @GetMapping("/page")
    public R<Page> getDishWithPage(PageInfo pageInfo) {
        Page<Dish> dishPage = new Page<>(pageInfo.getPage(), pageInfo.getPageSize());
        Page<DishDto> dishDtoPage = new Page<>(pageInfo.getPage(), pageInfo.getPageSize());

        LambdaQueryWrapper<Dish> dishQuery = new LambdaQueryWrapper<>();
        dishQuery.like(pageInfo.getName() != null, Dish::getName, pageInfo.getName())
                .orderByAsc(Dish::getSort)
                .orderByDesc(Dish::getUpdateTime);
        dishService.page(dishPage, dishQuery);

        BeanUtils.copyProperties(dishPage, dishDtoPage, "records");


        List<Dish> dishRecords = dishPage.getRecords();
        List<DishDto> dishDtoList = dishRecords.stream().map(item -> {
            DishDto dishDto = new DishDto();
            Long categoryId = item.getCategoryId();
            dishDto.setCategoryName(categoryServcie.getById(categoryId).getName());
            BeanUtils.copyProperties(item, dishDto);
            return dishDto;
        }).collect(Collectors.toList());
        dishDtoPage.setRecords(dishDtoList);

        return R.success(dishDtoPage);

    }
      @GetMapping("/{id}")
    public  R<DishDto> getDishAndFlavorById(@PathVariable Long id)
    {
        DishDto dishDto = new DishDto();
        BeanUtils.copyProperties(dishService.getById(id),dishDto);
        LambdaQueryWrapper<DishFlavor> dishFlavorQuery = new LambdaQueryWrapper<>();
        dishFlavorQuery.eq(DishFlavor::getDishId,dishDto.getId());
        dishDto.setFlavors(dishFlavorService.list(dishFlavorQuery));
        return  R.success(dishDto);
    }
       @PutMapping("")
       public R<String> updateDish(@RequestBody DishDto dishDto) {
           log.info("开始更新菜品");
           dishService.updateDishWithFlavor(dishDto);
           return R.success("更新成功");
       }
    @PostMapping("/status/{status}")
    public  R<String>  modifyStauts(@PathVariable(value = "status") Integer status, String ids){
        log.info("更新菜品状态");
        String[] id = ids.split(",");
        List<Dish> dishList =new ArrayList<>();
        for (String s : id) {
            Dish dish = new Dish();
            dish.setId(Long.valueOf(s));
            dish.setStatus(status);
            dishList.add(dish);
        }
        dishService.updateBatchById(dishList);
        return R.success("状态修改成功");
    }

//    http://localhost:8080/dish/list?categoryId=1397844263642378242
//    @GetMapping("/list")
//    public  R<List<Dish>> getDishByCategoryId(Dish dish){
//        LambdaQueryWrapper<Dish> dishQuery = new LambdaQueryWrapper<>();
//        dishQuery.eq(dish.getCategoryId()!=null,Dish::getCategoryId,dish.getCategoryId())
//                 .eq(Dish::getStatus,1)
//                  .orderByAsc(Dish::getSort).orderByDesc(Dish::getUpdateTime);
//        List<Dish> dishList = dishService.list(dishQuery);
//        return  R.success(dishList);
//    }
        @GetMapping("/list")
        public R<List<DishDto>> getDishByCategoryId(Dish dish) {
            LambdaQueryWrapper<Dish> dishQuery = new LambdaQueryWrapper<>();
            dishQuery.eq(dish.getCategoryId() != null, Dish::getCategoryId, dish.getCategoryId())
                    .eq(Dish::getStatus, 1)
                    .orderByAsc(Dish::getSort).orderByDesc(Dish::getUpdateTime);
            List<Dish> dishList = dishService.list(dishQuery);
             List<DishDto> dishDtoList =null ;


      dishDtoList=dishList.stream().map(item->{
          DishDto dishDto = new DishDto();
          BeanUtils.copyProperties(item,dishDto);
          Long dishId = item.getId();
                LambdaQueryWrapper<DishFlavor> dishFlavorQuery = new LambdaQueryWrapper<>();
                dishFlavorQuery.eq(DishFlavor::getDishId,dishId);
                List<DishFlavor> dishFlavors = dishFlavorService.list(dishFlavorQuery);
                dishDto.setFlavors(dishFlavors);
                return dishDto;
            }).collect(Collectors.toList());

            return R.success(dishDtoList);
        }


}
